package me.shurufa.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.activities.NewLoginActivity;
import me.shurufa.activities.NewUserCenterActivity;
import me.shurufa.bean.CommentEntity;
import me.shurufa.db.PersistenceUtils;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Utils;

/**
 * 评论列表适配器
 */
public class CommentListAdapter extends RecyclerView.Adapter<CommentListAdapter.ViewHolder> {

    private Context mContext;
    private List<CommentEntity> dataList;
    private OnCommentClickListener mOnCommentClickListener;

    /**
     * 点击item接口
     */
    public interface OnCommentClickListener {
        void onClick(View v, int position, CommentEntity commentEntity);
    }

    public CommentListAdapter(Context context, List<CommentEntity> dataList) {
        this.dataList = dataList;
        this.mContext = context;
    }

    @Override
    public CommentListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment, parent, false);
        return new CommentListAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(CommentListAdapter.ViewHolder holder, int position) {
        CommentEntity commentEntity = dataList.get(position);
        holder.bind(commentEntity);
        holder.divider.setVisibility(position == dataList.size() - 1 ? View.INVISIBLE : View.VISIBLE);
        bindListener(holder, position, commentEntity);
    }

    private void bindListener(ViewHolder holder, final int position, final CommentEntity comment) {
        holder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!handleLoginClick()) {
                    return;
                }
                if (null != mOnCommentClickListener) {
                    mOnCommentClickListener.onClick(v, position, comment);
                }

            }
        });

        holder.tv_uname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent in = new Intent(mContext, NewUserCenterActivity.class);
                NewUserCenterActivity.initArguments(in, comment.user_info, true);
                mContext.startActivity(in);
            }
        });
    }

    public void setOnCommentClickListener(OnCommentClickListener l) {
        this.mOnCommentClickListener = l;
    }

    /**
     * 尾部添加
     * @param items
     */
    public void append(List<CommentEntity> items) {
        if (Utils.isNullForList(items)) {
            return;
        }
        int pos = dataList.size();
        dataList.addAll(items);
        notifyItemRangeInserted(pos, items.size());
    }


    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public void remove(int position) {
        if (dataList.size() > 0) {
            dataList.remove(position);
            this.notifyItemRemoved(position);
        }
    }

    public void clear() {
        int size = dataList.size();
        dataList.clear();
        this.notifyItemRangeRemoved(0, size);
    }

    private boolean handleLoginClick() {
        boolean isLogin = PersistenceUtils.isLogin();
        Intent in = null;
        // 已登录 跳转到ProfileActivity
        if (isLogin) {
            return true;
        }

        // 未登录 则跳转至登录界面，点击登录时需要返回到当前界面
        else {
            in = new Intent(mContext, NewLoginActivity.class);
            in.setAction(Constants.ACTION_GO_LOGIN);
            EventBus.getDefault().postSticky(in);
            return false;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.item)
        View item;

        @Bind(R.id.iv_avatar)
        ImageView iv_avatar;

        @Bind(R.id.tv_uname)
        TextView tv_uname;

        @Bind(R.id.tv_create_time)
        TextView tv_create_time;

        @Bind(R.id.tv_comment_content)
        TextView tv_comment_content;

        @Bind(R.id.divider)
        View divider;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bind(CommentEntity comment) {

            // 头像
            if (comment.user_info != null && !TextUtils.isEmpty(comment.user_info.avatar)) {
                Glide.with(mContext).load(comment.user_info.avatar + Constants.THUMB_SUFFIX_AVATAR).asBitmap().centerCrop().into(new BitmapImageViewTarget(iv_avatar) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        super.setResource(resource);
                        RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(mContext.getResources(), resource);
                        circularBitmapDrawable.setCircular(true);
                        iv_avatar.setImageDrawable(circularBitmapDrawable);
                    }
                });
            }

            if (!TextUtils.isEmpty(comment.user_info.username)) {
                tv_uname.setText(comment.user_info.username);
            }

            // 时间
            tv_create_time.setText(Utils.getFormatTime(comment.created_at));
            tv_comment_content.setText(comment.content);
        }
    }
}
